package demo.mqserver.datacenter;

import demo.MqApplication;
import demo.mqserver.core.*;
import demo.mqserver.mapper.MetaMapper;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.List;

@Slf4j
public class DataBaseManager {

    //从 Spring 中拿到现成的对象
    private MetaMapper metaMapper;

    //针对数据库进行初始化
    public void init(){

        //手动获取到的 MetaMapper
        metaMapper = MqApplication.context.getBean(MetaMapper.class);

        //数据库不存在，建库建表
        if(!checkDBExists()){
            //创建一个 data 目录
            File dataDir = new File("./data");
            dataDir.mkdirs();
            //创建数据库
            createTable();
            //插入默认数据
            createDefaultData();
            log.info("[DataBaseManager] 数据库初始化完成！");
        }else{
            log.info("[DataBaseManager] 数据库已经存在！");
        }
    }

    public void deleteDB(){
        //使用 delete 删除目录，需要保证目录是空的
        File file = new File("./data/meta.db");
        boolean ret = file.delete();
        if(ret){
            log.info("[DataBaseManager] 删除数据库文件成功！");
        }else{
            log.info("[DataBaseManager] 删除数据库文件失败！");
        }

        File dataDir = new File("./data");
        ret = dataDir.delete();
        if(ret){
            log.info("data 目录，删除成功！");
        }else{
            log.info("data 目录，删除失败！");
        }
    }


    private boolean checkDBExists() {
        File file = new File("./data/meta.db");
        if(file.exists()){
            return true;
        }else{
            return false;
        }
    }

    private void createTable() {
        //建表操作不需要手动调用（不需要手动创建 meta.db 文件）
        //首次执行这里的数据库操作时，会自动创建出 mete.db 文件
        metaMapper.createExchangeTable();
        metaMapper.createQueueTable();
        metaMapper.createBindingTable();
        log.info("[DataBaseManager] 创建表完成！");
    }

    private void createDefaultData() {
        //添加一个默认的交换机
        //RabbitMQ 中的设定：带有一个匿名的交换机，类型是 DIRECT
        //构造一个默认的交换机
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        metaMapper.insertExchange(exchange);
        log.info("[DataBaseManager] 创建初始数据完成！");
    }




    //其他的数据库操作
    public void insertExchange(Exchange exchange){
        metaMapper.insertExchange(exchange);
    }
    public List<Exchange> selectAllExchanges(){
        return metaMapper.selectAllExchanges();
    }
    public void deleteExchange(String exchangeName){
        metaMapper.deleteExchange(exchangeName);
    }


    public void insertQueue(MSGQueue queue){
        metaMapper.insertQueue(queue);
    }
    public List<MSGQueue> selectAllQueues(){
        return metaMapper.selectAllQueues();
    }
    public void deleteQueue(String queueName){
        metaMapper.deleteQueue(queueName);
    }


    public void insertBinding(Binding binding){
        metaMapper.insertBinding(binding);
    }
    public List<Binding> selectAllBindings(){
        return metaMapper.selectAllBindings();
    }
    public void deleteBinding(Binding binding){
        metaMapper.deleteBinding(binding);
    }
}
